package com.sdry.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysRole;
import com.sdry.system.service.RoleService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @program: info_sdry
 * @description: 角色管理
 * @author: cb 2020-05-04
 * @create: 2020-02-12 19:37
 **/
@Controller
@RequestMapping(value = "role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 展示角色
     * @return
     */
    @RequestMapping("showRole")
    public String showRole() {
        logger.info("展示角色");
        return "system/role/roleList";
    }

    /**
     * 分页查询角色
     * @return
     */
    @GetMapping(value = "showRolePage")
    @ResponseBody
    public TableDataInfo showRolePage(SysRole role) {
        logger.info("获取角色列表 SysRole = " + JSONObject.fromObject(role));
        IPage<SysRole> sysRoleIPage = roleService.queryRolePage(role);
        return getDataTable(sysRoleIPage.getRecords(), sysRoleIPage.getTotal());
    }

    /**
     * 新增角色
     * @param sysRole
     * @param menus
     * @return
     */
    @PostMapping(value = "addRole")
    @ResponseBody
    public Integer addRole(SysRole sysRole, @RequestParam("menus[]") String[] menus) {
        logger.info("新增角色 sysRole = {}" + JSONObject.fromObject(sysRole));
        return roleService.addRole(sysRole, menus);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping(value = "deleteRole")
    @ResponseBody
    public Integer deleteRole(Long roleId) {
        logger.info("删除角色 roleId = " + roleId);
        return roleService.deleteRole(roleId);
    }

    /**
     * 去编辑页面
     * @param id 主键
     * @return
     */
    @RequestMapping(value = "updateRole" )
    @ResponseBody
    public String updateRole(Long id) {
        logger.info("去编辑页面 id = {}" + id);
        SysRole sysRole = null;
        if (id != null){
            sysRole = roleService.getById(id);
        }
        return JSONObject.fromObject(sysRole).toString();
    }

    /**
     * 修改角色
     * @param sysRole
     * @param menus
     * @return
     */
    @RequestMapping(value = "addOrUpdateRole" )
    @ResponseBody
    public Integer addOrUpdateRole(SysRole sysRole, @RequestParam(value = "menus[]",required = false) String[] menus) {
        logger.info("修改角色 sysRole = {}" + JSONObject.fromObject(sysRole));
        return roleService.addOrUpdateRole(sysRole, menus);
    }

    /**
     * 修改角色下app菜单
     * @param sysRole
     * @param menus
     * @return
     */
    @RequestMapping(value = "UpdateRoleAppMenu")
    @ResponseBody
    public Integer UpdateRoleAppMenu(SysRole sysRole, @RequestParam("menus[]") String[] menus) {
        logger.info("修改角色 sysRole = {}" + JSONObject.fromObject(sysRole));
        return roleService.UpdateRoleAppMenu(sysRole, menus);
    }
}
